home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: MegaDisc / MegaDisc 43 (1995-03)(MegaDisc Digital Publishing)(AU)(Disk 2 of 2)[m bamcopy].zip / MegaDisc 43 (1995-03)(MegaDisc Digital Publishing)(AU)(Disk 2 of 2)[m bamcopy].adf / Programming / Pascal_Tutes / Tute13.pas < prev    next >
Pascal/Delphi Source File  |  1995-01-27  |  7KB  |  210 lines

  1. {
  2.  
  3.              «»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»
  4.              «»                                                  «» 
  5.              «»                TUTORIAL THIRTEEN                 «» 
  6.              «»                                                  «» 
  7.              «»                        by                        «» 
  8.              «»                                                  «» 
  9.              «»                   Anthony Peck                   «» 
  10.              «»                                                  «» 
  11.              «»                                                  «» 
  12.              «»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«» 
  13.                                                                     
  14.  
  15.  
  16.          43  !¡  43  !¡  43  !¡  43  !¡  43  !¡  43  !¡  43  !¡  43
  17.  
  18.  
  19.                         Abandon scope all ye who enter... 
  20.  
  21.  
  22.     Isn't this procedure stuff more trouble than it's worth?  Nope,
  23.     but it does take some time to become familiar with some aspects of
  24.     procedural usage.
  25.  
  26.     The weirdest thing is the concept of local and global variables. 
  27.     Let's try to follow the logic of this next program, step by step...
  28.  
  29.     * We identify a global variable as 'x'.
  30.     * At the start of the program 'x' is set to a value of '1'.
  31.     * We call the procedure 'first'.
  32.     * First identifies a local variable called 'x'.
  33.     * The local first variable 'x' is set to a value of '5'.
  34.     * First passes 'x' to the procedure 'second'.
  35.     * Second changes 'x' to '10' and passes it back to 'first'.
  36.     * Back in the main program, the global variable 'x' still
  37.       value of '1'.
  38.     * The main program passes 'x' to the procedure 'second'.
  39.     * Second changes 'x' to '10' and passes it back to the main
  40.       program.
  41.  
  42.     Phew!!
  43.  
  44.     Check out diagram_five for the complete "picture".  This is an
  45.     animated version of the above flow chart.  Thanks to Chas Wyndham
  46.     for S_Anim5 (found on MD41).  }
  47.  
  48.     PROGRAM MDTute13;
  49.  
  50.     {    Program demonstrates local and global variables
  51.  
  52.                 Author : A N Peck
  53.  
  54.                 Date : 28 October 1994 
  55.                 
  56.  
  57.         Procedures used:
  58.     
  59.         First - defines local identifier x
  60.         
  61.         Second - defines local variable x  }    
  62.  
  63.  
  64.     Var x: byte;
  65.  
  66. {    ^
  67.     |
  68.     |__  The global variable "x" is defined.  }
  69.  
  70.     { ---------------------------------------------------------- }
  71.  
  72.     Procedure second(Var x: Byte);
  73.  
  74. {             ^
  75.              |
  76.              |__  This procedure is passed a variable which now
  77.                   can be changed by the procedure.  If the
  78.                   "Var" was missing, what do you think 
  79.                   would happen?  }  
  80.  
  81.     Begin
  82.  
  83.     x := 10;
  84.  
  85. {    ^
  86.     |
  87.     |__  This becomes a permanent change because 'x' was passed as a
  88.          "Var".  Isn't this just wonderful?  }
  89.     
  90.     Writeln;
  91.  
  92.     Writeln('     In Procedure second x equals ',x);
  93.  
  94.     Writeln;
  95.  
  96.     End; { second }
  97.  
  98.     { ---------------------------------------------------------- }
  99.  
  100.     Procedure first(x:byte);
  101.  
  102. {            ^
  103.             |
  104.             |__  This procedure identifies x as the byte value
  105.                  passed to it, but it cannot alter the global
  106.                  variable 'x'.  }
  107.       Begin
  108.  
  109.     x := 5;
  110.  
  111. {    ^
  112.     |
  113.     |__  This initialisation of 'x' is only valid within this
  114.          procedure, and those procedures which are called from here. 
  115.          This does not affect the global variable 'x'.  If you think
  116.          this is complicated, you should see my family tree!  }
  117.  
  118.      Writeln;
  119.  
  120.     Writeln('     In Procedure first, before calling second, x equals ',x);
  121.  
  122.     second(x);
  123.  
  124. {    ^
  125.     |
  126.     |__  "Second" is called, and 'x' is passed to the procedure.  }
  127.  
  128.     Writeln('     In Procedure first, after calling second, x equals ',x);
  129.     
  130.     Writeln;
  131.  
  132.     End; { first }
  133.  
  134.     { ---------------------------------------------------------- }
  135.     { ---------------------------------------------------------- }
  136.  
  137.     Begin { Main Program }
  138.     
  139.     x := 1;
  140.  
  141. {    ^
  142.     |
  143.     |__  Initialises global variable x as '1' }
  144.     
  145.     Writeln;
  146.  
  147.     Writeln('     Before calling any Procedures x equals ',x);
  148.  
  149.     first(x);
  150.  
  151. {    ^
  152.     |
  153.     |__  'x' is passed to the "first "procedure. }
  154.  
  155.     Writeln('     After calling first from the main program x equals ',x);
  156.     
  157.     second(x);
  158.  
  159. {    ^
  160.     |
  161.     |__  'x' is passed to "second", but this time it can be altered by
  162.          the procedure.  }
  163.  
  164.     Writeln('     After calling second from the main program x equals ',x);
  165.     
  166.     Writeln;
  167.     
  168.     End. { Main Program }
  169.  
  170.     { ---------------------------------------------------------- }
  171.  
  172. {    Ergonomic!  Check out the animation again, and try to follow
  173.     through the reasoning.  Of course, no idiot would define all the
  174.     variables within a program as 'x', but it does illustrate the point
  175.     that the compiler is often a wee bit smarter than the compilee! 
  176.  
  177.           
  178.  
  179.                                                      
  180.                                                     
  181.                                                    
  182.                                                   
  183.                                                  
  184.                                                   
  185.                                                 
  186.                                                
  187.                                               
  188.                                             
  189.                                           
  190.                                          
  191.                                          
  192.                                          
  193.                                           
  194.                                            
  195.                                     
  196.              «»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»
  197.              «»                                                  «» 
  198.              «»           cyclops quod erat demonstrandum        «» 
  199.              «»                                                  «» 
  200.              «»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«» 
  201.  
  202.  
  203.          43  !¡  43  !¡  43  !¡  43  !¡  43  !¡  43  !¡  43  !¡  43
  204.  
  205.  
  206.                                                                      }
  207.  
  208.  
  209.  
  210.